package com.a3dweijing.wetouch2.Util;

import android.telecom.Call;
import android.util.Log;

import com.a3dweijing.wetouch2.Common.CallBack;
import com.a3dweijing.wetouch2.base.App;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

/**
 * Created by Administrator on 2018/3/23.
 */

public class GaoDeMap {

    private CallBack callBack;

    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;

    //定位规则
    AMapLocationClientOption option = new AMapLocationClientOption();

    //声明定位回调监听器
    public AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            if (aMapLocation != null) {
                if (aMapLocation.getErrorCode() == 0) {
                    callBack.doNext(0, aMapLocation);
                } else {
                    callBack.doNext(-1, aMapLocation);
                    //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                    Log.e("AmapError", "location Error, ErrCode:"
                            + aMapLocation.getErrorCode() + ", errInfo:"
                            + aMapLocation.getErrorInfo());
                }
            }
        }
    };


    public AMapLocationClient createMap(CallBack callBack) {
        this.callBack = callBack;
        //初始化定位
        mLocationClient = new AMapLocationClient(App.getmContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);

        /**
         * 设置定位场景，目前支持三种场景（签到、出行、运动，默认无场景）
         */
        option.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);
        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置定位间隔,单位毫秒,默认为2000ms，最低1000ms。
        option.setInterval(1000);
        //设置是否返回地址信息（默认返回地址信息）
        option.setNeedAddress(true);
        //单位是毫秒，默认30000毫秒，建议超时时间不要低于8000毫秒。
        // option.setHttpTimeOut(20000);
        if (null != mLocationClient) {
            mLocationClient.setLocationOption(option);
            //设置场景模式后最好调用一次stop，再调用start以保证场景模式生效
            mLocationClient.stopLocation();
            mLocationClient.startLocation();
        }
        return mLocationClient;
    }

}
